﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Wolfy.NetDisk.Core.Model;
using Wolfy.NetDisk.Core.Repository;

namespace Wolfy.NetDisk.Data
{
    public class DeleteRepository<T> : IDeleteRepository<T> where T : DeleteEntity
    {
        protected readonly DbContext db;
        public DeleteRepository(IDbContextFactory factory)
        {
            this.db = factory.GetContext();
        }
        public IQueryable<T> Where(Expression<Func<T, bool>> predicate, bool showDeleted = false)
        {
            var res = this.db.Set<T>().Where(predicate);
            if (!showDeleted)
            {
                res = res.Where(o => o.IsDeleted == false);
            }
            return res;
        }

        public IQueryable<T> GetAll()
        {
            return this.db.Set<T>().Where(o => o.IsDeleted == false);
        }

        public void Restore(T entity)
        {
            entity.IsDeleted = false;
        }
    }
}
